//
//  MainIPhoneViewController.m
//  bsl-sdk
//
//  Created by Fanty on 13-12-2.
//
//

#import "CubeViewController.h"
#import "IsolatedStorageFile.h"
#import "AppDelegate.h"
#import "NavigationViewController.h"
#import "BSLCubeConstant.h"
@interface CubeViewController ()<UIWebViewDelegate>

@end

@implementation CubeViewController

-(id)init{
    self=[super init];
    
    if(self){
        _commandDelegate = [[CubeCommandDelegate alloc] initWithViewController:self];
        
    }
    return self;
}


- (void)viewDidLoad{
    [super viewDidLoad];
    self.webView.exclusiveTouch=YES;
    /*
    for(UIView* view in self.webView.subviews){
        if([view isKindOfClass:[UIScrollView class]]){
            UIScrollView* scrollView=(UIScrollView*)view;
            scrollView.showsHorizontalScrollIndicator=NO;
            scrollView.showsVerticalScrollIndicator=NO;
            scrollView.bounces=NO;
            //            scrollView.scrollEnabled=NO;
            break;
        }
    }
    */
}


-(void)loadFilePageWithFileUrl:(NSURL*)fileUrl{
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    self.startPage = [fileUrl absoluteString];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc{
    self.loadFinish=nil;
}

#pragma mark webvlew delegate

- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{
    
    
    NSURL* url = [request URL];
    NSString* fileUrl=[url absoluteString];
    NSLog(@"请求页面: %@", url);
    
    
    NSString* str=@"http://bsl-action/";
    NSRange range=[fileUrl rangeOfString:str];
    if (range.length>0){         //跳转指定页面
        
        fileUrl=[fileUrl substringFromIndex:range.location+range.length];

        [[NSURLCache sharedURLCache] removeAllCachedResponses];
        
        CubeViewController* controller=[[CubeViewController alloc] init];
        controller.title=self.title;
        
        [controller loadFilePageWithFileUrl:[NSURL fileURLWithPath:[IsolatedStorageFile cordovaFileForSearchPath:fileUrl]]];
        
        __weak CubeViewController* weakController=controller;
        __weak CubeNavigationViewController* navController=[AppDelegate appDelegate].navController;
        navController.view.userInteractionEnabled=NO;
        [controller setLoadFinish:^(BOOL success){
            [navController pushViewController:weakController animated:YES];
            navController.view.userInteractionEnabled=YES;
        }];
        
        return NO;
    }
    else if([fileUrl rangeOfString:@"http://pop"].length>0){        //后退当前页
        [self.navigationController popViewControllerAnimated:YES];
        return NO;
    }
    else if([fileUrl rangeOfString:@"http://home"].length>0){        //后退当前页
        [self.navigationController popToRootViewControllerAnimated:YES];
        return NO;
    }
    
    NSString *timeSp = [NSString stringWithFormat:@"?date=%ld", (long)[[NSDate date]  timeIntervalSince1970]];
    
    url =  [url URLByAppendingPathComponent:timeSp];
    
    NSURLRequest *newRequest = [NSURLRequest requestWithURL:url cachePolicy:[request cachePolicy] timeoutInterval:[request timeoutInterval]];
    
    return [super webView:theWebView shouldStartLoadWithRequest:newRequest navigationType:navigationType];
}


-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    if(self.loadFinish!=nil){
        self.loadFinish(YES);
        self.loadFinish=nil;
    }
    
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    if(self.loadFinish!=nil){
        self.loadFinish(NO);
        self.loadFinish=nil;
    }
}

#pragma mark method


@end



@implementation CubeCommandDelegate

/* To override the methods, uncomment the line in the init function(s)
 in MainViewController.m
 */

#pragma mark CDVCommandDelegate implementation

- (id)getCommandInstance:(NSString*)className{
    return [super getCommandInstance:className];
}

/*
 NOTE: this will only inspect execute calls coming explicitly from native plugins,
 not the commandQueue (from JavaScript). To see execute calls from JavaScript, see
 MainCommandQueue below
 */
//- (BOOL)execute:(CDVInvokedUrlCommand*)command
//{
//    return [super execute:command];
//}

- (NSString*)pathForResource:(NSString*)resourcepath;{
    return resourcepath;

}

@end



